package com.yvan.platform;

import com.yvan.Conv;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by YuanHua on 2017/6/9.
 */
public class ParserRequestMap extends HashMap {
    private static final SimpleDateFormat FORMAT_DATE = new SimpleDateFormat("yyyy-MM-dd");
    private static final SimpleDateFormat FORMAT_DATETIME = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private Map<String, String> innerMap = new HashMap<String, String>();

    public ParserRequestMap(Map<String, String[]> map) {
        super(map);
        if (map != null && map.size() > 0) {
            for (String key : map.keySet()) {
                innerMap.put(key, getParamString(key));
            }
        }
        putAll(innerMap);
    }

    public ParserRequestMap() {
    }

    public static Date strToDate(String str, int addDay, Date defaultVal) {
        return parseStrToDate(str, addDay, defaultVal, FORMAT_DATE);
    }

    public static Date strToDatetime(String str, int addDay, Date defaultVal) {
        return parseStrToDate(str, addDay, defaultVal, FORMAT_DATETIME);
    }

    private static Date parseStrToDate(String str, int addDay, Date defaultVal, SimpleDateFormat format) {
        if (org.apache.commons.lang.StringUtils.isBlank(str)) {
            return defaultVal;
        }
        try {
            Date date = format.parse(str);
            final Calendar calc = Calendar.getInstance();
            calc.setTime(date);
            calc.add(Calendar.DAY_OF_YEAR, addDay);
            return calc.getTime();
        } catch (ParseException e) {
            return defaultVal;
        }
    }

    public Object put(String key, String value) {
        super.put(key, value);
        return innerMap.put(key, value);
    }

    private String getParamString(String key) {
        return getParamString(key, null);
    }

    private String getParamString(String key, String defaultVal) {
        String[] params = (String[]) get(key);
        if (params == null || params.length == 0) return defaultVal;
        if (StringUtils.isNotBlank(params[0])) {
            return params[0].trim();
        }
        return defaultVal;
    }

    public String getString(String key) {
        return getString(key, null);
    }

    public String getString(String key, String defaultVal) {
        if (innerMap.get(key) != null) {
            return innerMap.get(key).toString().trim();
        }
        return defaultVal;
    }

    public Integer getInteger(String key) {
        return getInteger(key, null);
    }

    public Integer getInteger(String key, Integer defaultVal) {
        if (StringUtils.isNotBlank(key)) {
            return Conv.NIorNull(getString(key), null);
        }
        return defaultVal;
    }

    public Long getLong(String key) {
        return getLong(key, null);
    }

    public Long getLong(String key, Long defaultVal) {
        if (getString(key) != null) {
            return Conv.NLorNull(getString(key), null);
        }
        return defaultVal;
    }

    public Double getDouble(String key) {
        return getDouble(key, null);
    }

    public Double getDouble(String key, Double defaultVal) {
        if (getString(key) != null) {
            return Conv.NDBorNull(getString(key), null);
        }
        return defaultVal;
    }

    public BigDecimal getBigDecimal(String key) {
        return getBigDecimal(key, null);
    }

    public BigDecimal getBigDecimal(String key, BigDecimal defaultVal) {
        if (getString(key) != null) {
            return Conv.NDec(getString(key), null);
        }
        return defaultVal;
    }

    public Timestamp getDatetimeTimestamp(String key, int addDay, Date defaultVal) {
        Date date = null;
        if (getString(key) != null) {
            date = strToDatetime(getString(key), addDay, defaultVal);
        }
        if (date != null)
            return new Timestamp(date.getTime());
        return defaultVal == null ? null : new Timestamp(defaultVal.getTime());
    }

    public Timestamp getDateTimestamp(String key, int addDay, Date defaultVal) {
        Date date = null;
        if (getString(key) != null) {
            date = strToDate(getString(key), addDay, defaultVal);
        }
        if (date != null)
            return new Timestamp(date.getTime());
        return defaultVal == null ? null : new Timestamp(defaultVal.getTime());
    }

    @Override
    public String toString() {
        return "ParserRequestMap{" +
                "innerMap=" + innerMap +
                '}';
    }
}
